<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <tab-control class="tab-control"
                 :titles="titles"
                 @tabClick="tabClick"
                 ref="tabControlTop"
                 v-show="isTopFix"/>
    <scroll class="content"
            ref="scroll"
            :my-probe-type="3"
            @scroll="scrollContent"
            :my-pull-up-load="true"
            @pullingUp="loadMore">
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"/>
      <recommend-view :recommends="recommends"/>
      <feature-view/>
      <tab-control :titles="titles"
                   @tabClick="tabClick"
                   ref="tabControlBottom"/>
      <goods-list :goods="showGoods"/>
    </scroll>

    <back-top @click.native="backClick"
              v-show="isShowBackTop"/>
  </div>
</template>

<script>
import NavBar from "@/components/common/navBar/NavBar";
import TabControl from "@/components/content/tabControl/TabControl";
import GoodsList from "@/components/content/goods/GoodsList";
import Scroll from "@/components/common/scroll/Scroll";

import HomeSwiper from "@/views/home/childrenComponents/HomeSwiper";
import RecommendView from "@/views/home/childrenComponents/RecommendView";
import FeatureView from "@/views/home/childrenComponents/FeatureView";
import BackTop from "@/components/content/backTop/BackTop";

import {getMultiData, getMultiGoods} from "@/network/home";
import {debounce} from "@/common/utils";

export default {
  name: "Home",
  data() {
    return {
      banners: [],
      recommends: [],
      titles: ['流行', '新款', '精选'],
      goods: {
        'pop': {page: 0, list: []},
        'new': {page: 0, list: []},
        'sell': {page: 0, list: []}
      },
      currentType: 'pop',
      isShowBackTop: false,
      tabOffsetTop: 0,
      isTopFix: false,
      saveY: 0
    }
  },
  components: {
    NavBar,
    TabControl,
    HomeSwiper,
    RecommendView,
    FeatureView,
    GoodsList,
    Scroll,
    BackTop
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list
    }
  },
  created() {
    // 获取首页数据
    this.getMultiData()
    // 获取商品数据
    this.getMultiGoods('pop')
    this.getMultiGoods('sell')
    this.getMultiGoods('new')

  },
  mounted: function () {
    // 监听item图片加载完成
    let homeDebounce = debounce(this.$refs.scroll.refresh, 200);
    this.$bus.$on('itemImageLoad', () => {
      homeDebounce()
    })
  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 0)
    this.$refs.scroll.refresh()
  },
  deactivated() {
    this.saveY = this.$refs.scroll.getScrollY()
    // console.log(this.saveY);
  },
  methods: {
    /**
     * 事件监听相关方法
     */
    tabClick(index) {
      // console.log(index);
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
          break
      }
      this.$refs.tabControlTop.currentIndex = index
      this.$refs.tabControlBottom.currentIndex = index
    },
    backClick() {
      // .native 在我们需要监听一个组件的原生事件时，必须给对应事件加上.native修饰符，才能进行监听
      // console.log('backClick');
      this.$refs.scroll.scrollTo(0, 0, 500)
    },
    scrollContent(position) {
      // 判断backTop是否显示
      this.isShowBackTop = position.y < -1000

      // 判断tabControl是否吸顶
      this.isTopFix = position.y < -this.tabOffsetTop
    },
    loadMore() {
      // console.log('上拉加载更多');
      this.getMultiGoods(this.currentType)
    },
    swiperImageLoad() {
      // 获取tabControl的offsetTop
      // 所有组件都有一个$el,用于获取组件中的元素
      // console.log(this.$refs.tabControl.$el.offsetTop);
      this.tabOffsetTop = this.$refs.tabControlBottom.$el.offsetTop;
    },




    /**
     * 网络请求相关方法
     */
    getMultiData() {
      getMultiData().then(res => {
        // console.log('then')
        this.banners = res.data.banner.list
        this.recommends = res.data.recommend.list
      })
    },
    getMultiGoods(type) {
      const page = this.goods[type].page + 1
      getMultiGoods(type, page).then(res => {
        this.goods[type].list.push(...res.data.data.list)
        this.goods[type].page ++

        // 完成上拉加载更多
        this.$refs.scroll.finishPullUp()
      })

    }
  }
}
</script>

<style scoped>
  #home {
    /*padding-top: 44px;*/
    height: 100vh;

    position: relative;
  }

  .home-nav {
    background-color: var(--color-tint);
    color: #ffffff;

    /* 在使用浏览器原生滚动时，为了导航不跟随主页一起滚动 */
    /*position: fixed;*/
    /*left: 0;*/
    /*right: 0;*/
    /*top: 0;*/
    /*z-index: 9;*/
  }

  .content {
    /*height: 300px;*/
    overflow: hidden;

    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }

  /*.content {*/
  /*  height: calc(100% - 93px);*/
  /*  overflow: hidden;*/

  /*  margin-top: 44px;*/
  /*}*/

  .fixed {
    position: fixed;
    left: 0;
    right: 0;
    top: 44px;
  }

  .tab-control {
    position: relative;
    z-index: 9;
  }
</style>
